package com.flaskpy.web.eternity.core.holder;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class SpringContextHolder implements ApplicationContextAware, EnvironmentAware {
    private static ApplicationContext applicationContext;
    private static Environment environment;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext=applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        environment=environment;
    }

    /**
     * 跟据类型获了bean
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> type) {

        return applicationContext.getBean(type);
    }

    /**
     * 获取主属性文件key值
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return environment.getProperty(key);
    }


}
